package com.laker.admin.module.sys.service.storage;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laker.admin.framework.ext.mybatis.UserInfoAndPowers;
import com.laker.admin.framework.utils.EasyAdminSecurityUtils;
import com.laker.admin.module.sys.entity.SysFile;
import com.laker.admin.module.sys.service.ISysFileService;
import com.laker.admin.module.sys.util.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class StorageFacade {

    @Autowired
    ISysFileService sysFileService;
//    @Autowired
//    Storage storage;

    @Value("${imageUploadPath}")
    private String imageUploadPath;

    public SysFile store(MultipartFile file, String originalFilename) {
        // png/jpg
        String suffix = FileUtil.getSuffix(originalFilename);
        String fileName = IdUtil.fastSimpleUUID() + "." + suffix;

//        String filePath = storage.store(inputStream, contentLength, contentType, fileName);
        Map<String, String> uploadFile = FileUploadUtils.uploadFile(imageUploadPath, file);
        if (uploadFile == null || !uploadFile.containsKey("url")){
            throw new RuntimeException("文件上传失败");
        }
        SysFile sysFile = new SysFile();
        UserInfoAndPowers currentUserInfo = EasyAdminSecurityUtils.getCurrentUserInfo();
        sysFile.setUserId(currentUserInfo.getUserId());
        sysFile.setNickName(currentUserInfo.getNickName());
        sysFile.setFilePath(uploadFile.get("url"));
        sysFile.setFileName(originalFilename);
        sysFile.setCreateTime(LocalDateTime.now());
        sysFileService.save(sysFile);
        return sysFile;
    }

    public Page<SysFile> page(Page<SysFile> page, String keyWord) {
        LambdaQueryWrapper<SysFile> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(keyWord)) {
            queryWrapper.like(SysFile::getFileName, keyWord);
        }
        Page<SysFile> pageList = sysFileService.page(page, queryWrapper);
        List<SysFile> records = page.getRecords();
//        records.forEach(sysFile -> {
//            sysFile.setFilePath(storage.getUrl(sysFile.getFilePath()));
//        });
        return pageList;
    }

//    @Transactional
//    public void delete(Long id) {
//        SysFile sysFile = sysFileService.getById(id);
//        sysFileService.removeById(id);
//        storage.delete(sysFile.getFilePath());
//    }

//    @Transactional
//    public void delete(Long[] ids) {
//        for (Long id : ids) {
//            delete(id);
//        }
//    }
}
